package com.cqywc.cjadmin.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cqywc.cjadmin.pojo.Admin;
import com.cqywc.cjadmin.service.AdminSerivce;
import com.cqywc.cjadmin.service.ReturnSerivice;

@Controller
@RequestMapping("admin/member/")
public class AdminMemberController {

	@Autowired
	AdminSerivce adminService;

	@Autowired
	ReturnSerivice returnSerivce;
	/**
	 * 会员列表
	 * 
	 * @return
	 */

	@RequestMapping("memberLists")
	public String memberLists() {
		return "admin/member/memberLists";
	}

	@RequestMapping("memberListsData")
	@ResponseBody
	public ReturnSerivice memberListsData(Integer page, Integer limit, String username) {

		returnSerivce.setPage(page);
		returnSerivce.setLimit(limit);
		adminService.adminLists(returnSerivce, username);
		returnSerivce.setCode(0);
		return returnSerivce;
	}

	@RequestMapping("addMember")
	public String addMember() {
		
		
		return "admin/member/addMember";
	}
	
	@RequestMapping("editMemberSave")
	@ResponseBody
	public ReturnSerivice addMemberSave(Admin admin ,BindingResult result) {
		int row=0;
		if(result.hasErrors()) {
			List<ObjectError> allErrors = result.getAllErrors();
			String msgString =allErrors.get(0).getDefaultMessage();
			returnSerivce.setCode(100);
			returnSerivce.setMsg(msgString);
			return returnSerivce;
		}
		try {
			row= adminService.saveAdmin(admin);
		} catch (Exception e) {
			returnSerivce.setCode(100);
			returnSerivce.setMsg(e.getMessage());
			return returnSerivce;
		}
		if(row>0) {
			returnSerivce.setCode(0);
			returnSerivce.setMsg("ok");
		}else {
			returnSerivce.setCode(100);
			returnSerivce.setMsg("失败");
		}
		return this.returnSerivce;
	}
	
	@RequestMapping(value="edit/{id}",method=RequestMethod.GET)
	public ModelAndView editMember(@PathVariable  Integer id) throws Exception {
		
		Admin admin = adminService.getAdminById(id);
		ModelAndView modelAndView =new ModelAndView();
		modelAndView.addObject("admin", admin);
		modelAndView.setViewName("admin/member/editMember");
		
		return modelAndView;
	}
	
	@ResponseBody
	@RequestMapping(value="editMemberSave",method=RequestMethod.POST)
	public ReturnSerivice editMemberSave(Admin admin ,BindingResult result) {
		int row=0;
		if(result.hasErrors()) {
			List<ObjectError> allErrors = result.getAllErrors();
			String msgString =allErrors.get(0).getDefaultMessage();
			returnSerivce.setCode(100);
			returnSerivce.setMsg(msgString);
			return returnSerivce;
		}
		try {
			row= adminService.updateAdmin(admin);
		} catch (Exception e) {
			returnSerivce.setCode(100);
			returnSerivce.setMsg(e.getMessage());
			return returnSerivce;
		}
		if(row>0) {
			returnSerivce.setCode(0);
			returnSerivce.setMsg("ok");
		}else {
			returnSerivce.setCode(100);
			returnSerivce.setMsg("失败");
		}
		return returnSerivce;
	}
	@ResponseBody
	@RequestMapping("test")
	public Admin test() {
		Admin admin = returnSerivce.test();
		return admin;
		
	}

	
	
	

}
